package cn.edu.glut.user.service.impl;

import java.util.Collection;
import java.util.Date;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import cn.edu.glut.entity.Teacher;
import cn.edu.glut.user.repository.StudentRepository;
import cn.edu.glut.user.repository.TeacherRepository;
import cn.edu.glut.user.service.TeacherService;
import cn.edu.glut.vo.TeacherToAdminVO;

@Service
public class TeacherServiceImpl implements TeacherService {
	@Autowired
	TeacherRepository teacherRepository;

	@Override
	public int save(Teacher t) {
		teacherRepository.save(t);
		if(!StringUtils.isEmpty(t.getId()))
			return 1;
		return 0;
	}

	@Override
	public void save(Collection<Teacher> studentList) {
		teacherRepository.save(studentList);
	}

	@Override
	@Transactional
	public void deleteAll() {
		teacherRepository.deleteAllInBatch();
	}

	@Override
	public void delete(String id) {
		teacherRepository.delete(id);
	}

	@Override
	public int update(Teacher t) {
		if(StringUtils.isEmpty(t.getId()))
			return 0;
		Preconditions.checkNotNull(teacherRepository.findOne(t.getId()),"该老师不存在");
		teacherRepository.save(t);
		return 1;
	}

	@Override
	public Teacher findById(String id) {
		return teacherRepository.findOne(id);
	}

	@Override
	public Page<TeacherToAdminVO> list(Pageable pageable) {
		return teacherRepository.findAllTeacherVO(pageable);
	}

	@Override
	public TeacherToAdminVO findTeacherToAdminVOByUsername(String username) {
		return teacherRepository.findTeacherToAdminVOByUsername(username);
	}

	@Override
	public Page<TeacherToAdminVO> list(Pageable pageable, String studentId) {
		return teacherRepository.findAllTeacherVO(pageable, studentId);
	}

}
